package cn.cvs.eduservice.service.impl;

import cn.cvs.eduservice.entity.EduTeacher;
import cn.cvs.eduservice.entity.vo.TeacherQuery;
import cn.cvs.eduservice.mapper.EduTeacherMapper;
import cn.cvs.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@CacheConfig(cacheNames = "eduTeacher")
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();


        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }
        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    @CacheEvict(allEntries = true)
    public boolean removeTeacherById(String id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public boolean saveTeacher(EduTeacher teacher) {
        rabbitTemplate.convertAndSend("lingqi-direct", "direct", teacher);
        return true;
    }

    @RabbitListener(queues = "teacher_queue")
    @CacheEvict(allEntries = true)
    public void toSave(EduTeacher eduTeacher) {
        baseMapper.insert(eduTeacher);
    }


    @Override
    @CacheEvict(allEntries = true)
    public boolean updateTeacherById(EduTeacher teacher) {
        return baseMapper.updateById(teacher) > 0;
    }

    @Override
    @Cacheable(key = "'findAll'")
    public List<EduTeacher> findAll() {
        return baseMapper.selectList(null);
    }

    /*
    肆虐的寒冰 再次来临 来宾 排着来听
    来劲 派兵入侵你的腹地 竖起中指
    注意图纸 目的处死 姆的 模子
    不止如此 主旨冒进新的路子
    造诣 早已不只是那跑的兔子（rabbit run）
    现在寒冬将至 寒风将刺 狂轰三次 娘炮干死
    臂膀长至 B榜央视 计划强制 替换圈子
    弊端彼此不给面子 管你戏子天子
    是否有链子 是否pass 怕死 必定被揪小辫子
    小骗子 别秀小点子 我是老鹰 你是小燕子
    给掉链子的 淹死 烧点纸 悼念词是天职
    而偏执的大佬法老要发飙 发招 耍捣 法宝 打跑了傻叼
    我首次手稿 不为收藏手表 而是想做最硬派的说唱首
    我坚持法式空手道 你拿武器 而我只用手刀*/
}
